<?php
class Node extends Module {
    private $node_id;
    private $types = array();
    
    public function act_display() {
        $node_id = (int) tos_http_get('node_id');
        $url = '';
        if ($node_id <= 0) {
            $parent_id = (int) tos_http_get('parent_id');
            if ($parent_id > 0) {
                $url = $this->model('node')->get_url($parent_id);
            }
        } else {
            $url = $this->model('node')->get_url($node_id);
        }
        tos_router_redirect(array('url' => $url), true);
    }
    
    
    
    public function add_types($types = array()) {
        if ( ! $types) {
            return false;
        }
        
        if ( ! $this->types) {
            $this->types = $types;
        } else {
            $this->types = array_merge($this->types, $types);
            $this->types = array_unique($this->types);
        }
        
        return true;
    }
    
    public function add_type($type = '') {
        $type = (string) $type;
        $type = trim($type);
        if (empty($type)) {
            return false;
        }
        if (in_array($type, $this->types)) {
            return false;
        } else {
            $this->types[] = $type;
            return true;
        }
    }
    
    
    public function get_types() {
        return $this->types;
    }
    
    
    public function get_node_nav($node_id) {
        return $this->model('node')->get_node_nav($node_id);
    }
    
    
    public function block_node_nav($node_id) {
        $menu_items = $this->model('node')->get_node_nav($node_id);
        return $this->view('node/nav', array('menu_items' => $menu_items));
    }
    
    
    public function block_menu($parent_id, $selected_node_id = 0, $offset = 0, $limit = 10, $base_url = '') {
        $offset = (int) $offset;
        $limit = (int) $limit;
        
        $menu_items = $this->model('node')->get_menu_items($parent_id, $offset, $limit);
        return $this->view('node/menu', 
            array(
                'menu_items' => $menu_items, 
                'selected_node_id' => $selected_node_id,
                'base_url' => $base_url//$this->model('node')->get_url_segment($parent_id)
            ));
    }
    
    public function block_add($data = array()) {
        /*$out = out();
        $out->add_js_script($this->view('node/edit'));*/
        
        return $this->view('node/edit', $data);
    }
    
    
    public function block_edit($data = array()) {
        $model_node = $this->model('node');
        $node = $model_node->get_node_by_id($data['node_id']);
        $data = array_merge($data, $node);
        $data['keyword_list'] = $model_node->get_keywords_from_node($data['node_id']);
        //$data['keyword_list'] = $this->model('node')->get_keywords($words);
        return $this->view('node/edit', $data);
    }
    
    
    public function block_edit_script() {
        return $this->view('node/edit');
    }
    
    public function block_add_options() {
        $types = $this->get_types();
        return $this->view('node/add_options', array('type_list' => $types));
    }
    
    
    public function block_all_nodes($offset = 0, $limit = 20) {
        
    }
    
    
    /*public function block_search() {
        $model_node = $this->model('node');
        
        //$nodes = $model_node->search_nodes();
        $nodes = $model_node->get_nodes();
        return $this->view('node/search', array('nodes' => $nodes));
    }*/
    
    
    
    public function block_nodes($offset, $limit) {
        $nodes = $this->model('node')->get_nodes($offset, $limit);
        return $this->view('node/nodes', array('nodes' => $nodes));
    }
    
    public function block_nodes_by_search($q = ''){
        $nodes = $this->model('node')->get_nodes_by_search($q);
        return $this->view('node/nodes', array('nodes' => $nodes));
    }
    
    
    public function block_nodes_by_parent_id($parent_id) {
        $page_index = (int) tos_http_get('page');
        $page_index = ($page_index <= 0) ? 1 : $page_index;
        $page_length = 11;
        $offset = ($page_index - 1) * $page_length;
        $child_nodes = $this->model('node')->get_nodes_by_parent_id($parent_id, $offset, $page_length);
        
        $block = $this->view('node/nodes', array('nodes' => $child_nodes));
        $block->add_block(m('html')->block_pagination(array(
            'page_index' => $page_index,
            'page_count' => ceil($this->model('node')->get_count_by_parent_id($parent_id) / $page_length)
        )), 'pagination');
        
        return $block;
    }
    
    
    public function block_nodes_by_keyword_id($keyword_id) {
        $nodes = $this->model('node')->get_nodes_by_keyword_id($keyword_id);
        return $this->view('node/nodes', array('nodes' => $nodes, 'attribute' => array('description_length' => 75)));
    }
    
    
    public function block_nodes_top() {
        $root_nodes = $this->model('node')->get_nodes_by_parent_id(0, 0, 1);
        if ( ! $root_nodes) tos_die();
        $root_node = array_shift($root_nodes);
        $root_node_id = $root_node['id'];
        $nodes = $this->model('node')->get_nodes_by_parent_id($root_node_id, 0, 7);
        
        $nodes = array_merge($root_nodes, $nodes);
        return $this->view('node/nodes', 
            array('nodes' => $nodes, 'attribute' => array('description_length' => 75, 'show_keywords' => 0)));
    }
    
    
    public function block_nodes_hot() {
        $nodes = $this->model('node')->get_nodes_hot_hit();
        return $this->view('node/nodes', 
            array('nodes' => $nodes, 'attribute' => array('description_length' => 75, 'show_keywords' => 0)));
    }
    
    
    public function block_nodes_by_word($word) {
        /*$word = trim($word);
        $word = strtolower($word);
        $model_node = $this->model('node');
        $keyword_id = */
        $nodes = $this->model('node')->get_nodes_by_word($word);
        return $this->view('node/nodes', array('nodes' => $nodes));
    }
    
    public function block_prev_nodes($node_id) {
        $prev_nodes = $this->model('node')->get_prev_nodes($node_id);
        return $this->view('node/nodes', array('nodes' => $prev_nodes));
    }
    
    public function block_next_nodes($node_id) {
        $prev_nodes = $this->model('node')->get_next_nodes($node_id);
        return $this->view('node/nodes', array('nodes' => $prev_nodes));
    }
    
    public function block_keywords($q = '', $label = '') {
        $model_node = $this->model('node');
        $label = trim($label);
        
        $selected_keyword = null;
        if ( ! empty($label)) {
            $selected_keyword = $model_node->get_keyword_by_word($label);
        }
        
        $keywords = $model_node->get_keywords_like_word($q);
        //print_r($keywords);
        if ($selected_keyword) {
            $selected_id = $selected_keyword['id'];
            if (isset($keywords[$selected_id])) {
                $keywords[$selected_id]['selected'] = true;
            } else {
                $selected_keyword['selected'] = true;
                $keywords[$selected_id] = $selected_keyword;
            }
        }
        
        return $this->view('node/keyword', array('keywords' => $keywords));
    }
    
    /*public function block_keywords($keyword_str, $onclick = '') {
        $words = explode(',', $keyword_str);
        $keywords = $this->model('node')->get_keywords($words);
        return $this->view('node/keyword', array('keywords' => $keywords, 'onclick' => $onclick));
    }*/
    
    
    public function block_node_keywords($node_id, $onclick = '') {
        $keywords = $this->model('node')->get_keywords_from_node($node_id);
        return $this->view('node/node_keyword', array('keywords' => $keywords, 'onclick' => $onclick));
    }
    
    
    public function block_display($params = array()) {
        $node_url = isset($params['node_url']) ? $params['node_url'] : '';
        $parent_url = isset($params['parent_url']) ? $params['parent_url'] : '';
        if (empty($node_url)) tos_die('error request');
        
        $model_node = $this->model('node');
        $parent_node = array();
        $parent_id = 0;
        if ( ! empty($parent_url)) {
            $parent_node = $model_node->get_node_by_url_segment($parent_url);
            
            if (isset($parent_node['id'])) {
                $parent_id = $parent_node['id'];
            }
        }
        $node = $model_node->get_node_by_url_segment($node_url);
        if ($parent_node) {
            $node['parent'] = $parent_node;
        }
        $block_display_node = $this->view('node/display', $node);
        $block_display_content = m($node['type'])->block_display_content($node['id']);
        $block_display_node->add_block($block_display_content);
        
        return $block_display_node;
    }
    
    
    
    
    public function block_node($node, $attribute = array()) {
        if (is_int($node)) {
            $node = $this->model('node')->get_node_by_id($node);
        }
        return $this->view('node/node', array('node' => $node, 'attribute' => $attribute));
    }
    
    
    public function block_display_content_by_url_segment($url_segment = '') {
        $url_segment = trim($url_segment);
        if (empty($url_segment)) tos_die();
        
        $model_node = $this->model('node');
        $node = $model_node->get_node_by_url_segment($url_segment);
        if ( ! $node) tos_die();
        
        $out = out();
        $out->add_title($node['caption']);
        $out->add_description($node['description']);
        $keywords = $model_node->get_keywords_from_node($node['id'], 0);
        foreach ($keywords as $keyword) {
            $out->add_keywords($keyword['word']);
        }
        
        $block_display_content = m($node['type'])->block_display_content($node['id']);
        $block_display_content->add_data('caption', $node['caption']);
        $block_display_content->add_data('node_id', $node['id']);
        $block_display_content->add_data('url_segment', $node['url_segment']);
        $block_display_content->add_data('parent_id', $node['parent_id']);
        
        return $block_display_content;
    }
    
    
    
    public function block_display_by_url_segment($url_segment = '') {
        $url_segment = trim($url_segment);
        if (empty($url_segment)) tos_die();
        
        $model_node = $this->model('node');
        $node = $model_node->get_node_by_url_segment($url_segment);
        
        $out = out();
        $out->add_title($node['caption']);
        $out->add_description($node['description']);
        $keywords = $model_node->get_keywords_from_node($node['id'], 0);
        foreach ($keywords as $keyword) {
            $out->add_keywords($keyword['word']);
        }
        
        if ( ! $node) tos_die();
        $block_display_node = $this->view('node/display', $node);
        $block_display_content = m($node['type'])->block_display_content($node['id']);
        $block_display_node->add_block($block_display_content);
        
        return $block_display_node;
        
    }
    
    
    public function block_synopsis($node_id, $desc_length = 100) {
        $node = $this->model('node')->get_node_by_id($node_id);
        if ($node) {
            return $this->view('node/synopsis', 
                array('node' =>$node, 'desc_length' => $desc_length));
        }
    }
    
    public function block_synopsis_node($node) {
        return $this->view('node/synopsis', $node);
    }
    
    
    public function add_node_data($type = '') {
        $type = trim($type);
        if (empty($type)) tos_die();
        
        $post = $_POST;
        if ($post) {
            $post['type'] = $type;
            $post['user_id'] = m('user')->get_user_id();
            $parent_id = (int) tos_http_post('parent_id');
            if ($parent_id <= 0) {
                $parent_id = (int) tos_http_get('parent_id');
            }
            $post['parent_id'] = $parent_id;
            
            $node_id = $this->model('node')->add_node($post);
            return $node_id;
        }
        return 0;
    }
    
    public function old_add_data($type = '') {
        $type = (string) $type;
        $type = trim($type);
        if (empty($type)) {
            tos_die();
        }
        
        $caption = tos_http_post('caption');
        $url_segment = tos_http_post('url_segment');
        $description = tos_http_post('description');
        $keywords = tos_http_post('keyword');
        $keyword_ids = tos_http_post('keyword_id');
        $prev_ids = tos_http_post('prev_id');
        $thumbnail = tos_http_post('thumbnail');
        $is_public = (0 === (int) tos_http_post('is_public')) ? 0 : 1;
        
        $user_id = m('user')->get_user_id();
        
        $node_id = $this->model('node')->add_node($user_id, $type, $caption, $keywords, $keyword_ids,
            $prev_ids, $description, $url_segment, $is_public, $thumbnail);
        
        return $node_id;
        /*
         * $user_id = 0, $type = 0, $caption = '',
        $keywords = array(), $keyword_ids = array(), $prev_ids = array(),
        $description = '', $url_segment = '', $is_public = 0, $thumbnail = ''
         * */
    }
    
    
    public function remove_node($node_id) {
        return $this->model('node')->remove_node($node_id);
    }
    
    public function edit_node_data($node_id) {
        $node_id = (int) $node_id;
        if ($node_id <= 0) tos_die();
        $post = $_POST;
        if ($post) {
            return $this->model('node')->edit_node($node_id, $post);
        }
        return false;
    }
    
    
    public function get_url_segment($node_id) {
        return $this->model('node')->get_url_segment($node_id);
    }
    
    
    public function get_caption($node_id) {
        return $this->model('node')->get_caption($node_id);
    }
    
    public function block_search_bar() {
        return $this->view('ajax/search_bar');
    }
    
    public function ajax_search() {
        header("Cache-Control: no-cache, must-revalidate");
        $word = tos_http_get('q');
        $word = urldecode($word);
        $model_node = $this->model('node');
        $keywords = $model_node->search_keywords($word);
        $this->view('ajax/search', array('keywords' => $keywords))->display();
    }
    
    public function ajax_link_search() {
        header("Cache-Control: no-cache, must-revalidate");
        $word = tos_http_get('q');
        $word = urldecode($word);
        $model_node = $this->model('node');
        $keywords = $model_node->search_keywords($word);
        $this->view('ajax/link_search', array('keywords' => $keywords))->display();
    }
    
    
    public function json_search_keywords() {
        //sleep(3);
        header("Cache-Control: no-cache, must-revalidate");
        $word = tos_http_get('word');
        $word = urldecode($word);
        $keywords = $this->model('node')->search_keywords($word);
        if ($keywords) {
            echo json_encode($keywords);
        } else {
            echo '';
        }
    }
    
    public function json_search_nodes_by_keyword_id() {
        header("Cache-Control: no-cache, must-revalidate");
        $keyword_id = (int) tos_http_get('keyword_id');
        if ($keyword_id > 0) {
            $model_node = $this->model('node');
            $nodes = $model_node->get_nodes_by_keyword_id($keyword_id);
            
            $json_nodes = array();
            
            foreach ($nodes as $node) {
                $json_nodes[$node['id']] = $this->block_node($node, array('description_length' => 75))->fetch('html');
                /*$thumbnail = trim($node['thumbnail']);
                $thumbnail = empty($thumbnail) ? 'img/icon/blueprint.png' : $thumbnail;
                $node['thumbnail'] = tos_img_src($thumbnail, 60, 40); */
            }
            if ($json_nodes) {
                echo json_encode($json_nodes);
            } else {
                echo '';
            }
        }
    }
    
    
    public function json_prev_nodes($node_id = 0) {
        $node_id = (int) $node_id;
        if ($node_id <= 0) $node_id = (int) tos_http_get('node_id');
        if ($node_id <= 0) {
            echo '{}';
            return;
        }
        
        $prev_nodes = $this->model('node')->get_prev_nodes($node_id);
        $json_nodes = array();
        foreach ($prev_nodes as $node) {
            $json_nodes[$node['id']] = $this->block_node($node, array('description_length' => 75))->fetch('html');
        }
        if ($json_nodes) {
            echo json_encode($json_nodes);
        } else {
            echo '{}';
        }
    }
    
    public function json_synopsis() {
        $node_id = (int) tos_http_get('node_id');
        $node = $this->model('node')->get_node_by_id($node_id);
        if ($node){
            echo json_encode($node);
        } else {
            echo '';
        }
    }
}
?>